<?php
namespace app\admin666\controller;

use think\Request;
use think\Db;

class Group extends Admin
{
    /**
    * 分组
    * @date: 2018年6月7日 上午10:26:48
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function index(){
	    $map = [];
	    $q = input('get.q','');
	    if($q != ''){
	    	$map['title'] = ['like',"%{$q}%"];
	    }
		$order="id desc";
		
		$list = Db::name('auth_group')->where($map)->paginate(10);
		$page = $list->render();
		
		$this->assign('_list', $list);
		$this->assign('_page', $page);
		
		return $this->fetch();
    }
    
    /**
    * 添加分组
    * @date: 2018年6月7日 上午10:32:54
    * @author: xiaocui
    * @param: $GLOBALS
    * @return:
    */
    public function add(){
        if(Request::instance()->isPost()){
            $model = model('AuthGroup');
            $data = input('post.');
            $data['rules'] = implode(',', $data['rules']);
            if($model->save($data)){
                $this->success('添加成功！','');
            }else{
                $this->error($model->getError());
            }
    
        }else{
            $mgroup = [
                ['id'=>1,'title'=>'管理后台','rid'=>'25'],
                ['id'=>2,'title'=>'代理系统','rid'=>'26'],
            ];
            	
            $menulist[1] = model('AuthGroup')->getRules(1);
            $menulist[2] = model('AuthGroup')->getRules(2);
            	
            $this->assign('menulist',$menulist);
            $this->assign('mgroup',$mgroup);
            return $this->fetch('edit');
        }
    }
    
    /**
    * 编辑分组
    * @date: 2018年6月7日 上午10:50:46
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function edit($id=0){
        if(Request::instance()->isPost()){
            $model = model('AuthGroup');
            $data = input('post.');
            $data['rules'] = implode(',', $data['rules']);
            if($model->save($data,['id'=>$id])){
                $this->success('编辑成功！','');
            }else{
                if($model->getError()){
                    $this->error($model->getError());
                }else{
                    $this->success('编辑成功！','');
                }
            }
        }else{
            $mgroup = [
                ['id'=>1,'title'=>'管理后台','rid'=>'25'],
                ['id'=>2,'title'=>'代理系统','rid'=>'26'],
            ];
            	
            $info = model('AuthGroup')->find($id);
            $this->assign('info',$info);
            $menulist[1] = model('AuthGroup')->getRules(1);
            $menulist[2] = model('AuthGroup')->getRules(2);
            $this->assign('menulist',$menulist);
            $this->assign('mgroup',$mgroup);
            return $this->fetch();
        }
    }
    
    /**
    * 删除分组
    * @date: 2018年6月7日 上午10:50:56
    * @author: xiaocui
    * @param: $GLOBALS
    * @return:
    */
    public function del($id=0){
        $model = model('AuthGroup');
        if(($model->destroy($id))){
            //记录行为
            action_log('del_user_group', 'member', $id, UID);
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }
}
